<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="业务员" prop="salesman">
        <el-input
          v-model="queryParams.salesman"
          placeholder="请输入业务员"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="收款编号" prop="collectionNo">
        <el-input
          v-model="queryParams.collectionNo"
          placeholder="请输入收款编号"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="收款日期" prop="collectionDate">
        <el-date-picker clearable size="small"
          v-model="queryParams.collectionDate"
          type="date"
          value-format="yyyy-MM-dd"
          placeholder="选择收款日期">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="客户名称" prop="customerName">
        <el-input
          v-model="queryParams.customerName"
          placeholder="请输入客户名称"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="源单" prop="source">
        <el-input
          v-model="queryParams.source"
          placeholder="请输入源单"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="源单金额" prop="sourceAc">
        <el-input
          v-model="queryParams.sourceAc"
          placeholder="请输入源单金额"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="源单已收金额" prop="sourceAcy">
        <el-input
          v-model="queryParams.sourceAcy"
          placeholder="请输入源单已收金额"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="费用合计" prop="expenseTotal">
        <el-input
          v-model="queryParams.expenseTotal"
          placeholder="请输入费用合计"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="销售业绩" prop="salesPerform">
        <el-input
          v-model="queryParams.salesPerform"
          placeholder="请输入销售业绩"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="产品成本合计" prop="productCost">
        <el-input
          v-model="queryParams.productCost"
          placeholder="请输入产品成本合计"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="佣金合计" prop="commissionCost">
        <el-input
          v-model="queryParams.commissionCost"
          placeholder="请输入佣金合计"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="目标额" prop="targetAc">
        <el-input
          v-model="queryParams.targetAc"
          placeholder="请输入目标额"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="目标完成率" prop="targetRate">
        <el-input
          v-model="queryParams.targetRate"
          placeholder="请输入目标完成率"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="提成比例" prop="percentRate">
        <el-input
          v-model="queryParams.percentRate"
          placeholder="请输入提成比例"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="业绩提成" prop="performAc">
        <el-input
          v-model="queryParams.performAc"
          placeholder="请输入业绩提成"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="本次收款金额" prop="cuCollectionAc">
        <el-input
          v-model="queryParams.cuCollectionAc"
          placeholder="请输入本次收款金额"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['crm:sales:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['crm:sales:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['crm:sales:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['crm:sales:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="salesList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="业务员" align="center" prop="salesman" />
      <el-table-column label="收款编号" align="center" prop="collectionNo" />
      <el-table-column label="收款日期" align="center" prop="collectionDate" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.collectionDate, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="客户名称" align="center" prop="customerName" />
      <el-table-column label="源单" align="center" prop="source" />
      <el-table-column label="源单金额" align="center" prop="sourceAc" />
      <el-table-column label="源单已收金额" align="center" prop="sourceAcy" />
      <el-table-column label="本次收款金额" align="center" prop="cuCollectionAc" />
      <el-table-column label="费用合计" align="center" prop="expenseTotal" />
      <el-table-column label="销售业绩" align="center" prop="salesPerform" />
      <el-table-column label="产品成本合计" align="center" prop="productCost" />
      <el-table-column label="佣金合计" align="center" prop="commissionCost" />
      <el-table-column label="目标额" align="center" prop="targetAc" />
      <el-table-column label="目标完成率" align="center" prop="targetRate" />
      <el-table-column label="提成比例" align="center" prop="percentRate" />
      <el-table-column label="业绩提成" align="center" prop="performAc" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['crm:sales:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['crm:sales:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改销售报表对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="业务员" prop="salesman">
          <el-input v-model="form.salesman" placeholder="请输入业务员" />
        </el-form-item>
        <el-form-item label="收款编号" prop="collectionNo">
          <el-input v-model="form.collectionNo" placeholder="请输入收款编号" />
        </el-form-item>
        <el-form-item label="收款日期" prop="collectionDate">
          <el-date-picker clearable size="small"
            v-model="form.collectionDate"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="选择收款日期">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="客户名称" prop="customerName">
          <el-input v-model="form.customerName" placeholder="请输入客户名称" />
        </el-form-item>
        <el-form-item label="源单" prop="source">
          <el-input v-model="form.source" placeholder="请输入源单" />
        </el-form-item>
        <el-form-item label="源单金额" prop="sourceAc">
          <el-input v-model="form.sourceAc" placeholder="请输入源单金额" />
        </el-form-item>
        <el-form-item label="源单已收金额" prop="sourceAcy">
          <el-input v-model="form.sourceAcy" placeholder="请输入源单已收金额" />
        </el-form-item>
        <el-form-item label="费用合计" prop="expenseTotal">
          <el-input v-model="form.expenseTotal" placeholder="请输入费用合计" />
        </el-form-item>
        <el-form-item label="销售业绩" prop="salesPerform">
          <el-input v-model="form.salesPerform" placeholder="请输入销售业绩" />
        </el-form-item>
        <el-form-item label="产品成本合计" prop="productCost">
          <el-input v-model="form.productCost" placeholder="请输入产品成本合计" />
        </el-form-item>
        <el-form-item label="佣金合计" prop="commissionCost">
          <el-input v-model="form.commissionCost" placeholder="请输入佣金合计" />
        </el-form-item>
        <el-form-item label="目标额" prop="targetAc">
          <el-input v-model="form.targetAc" placeholder="请输入目标额" />
        </el-form-item>
        <el-form-item label="目标完成率" prop="targetRate">
          <el-input v-model="form.targetRate" placeholder="请输入目标完成率" />
        </el-form-item>
        <el-form-item label="提成比例" prop="percentRate">
          <el-input v-model="form.percentRate" placeholder="请输入提成比例" />
        </el-form-item>
        <el-form-item label="业绩提成" prop="performAc">
          <el-input v-model="form.performAc" placeholder="请输入业绩提成" />
        </el-form-item>
        <el-form-item label="本次收款金额" prop="cuCollectionAc">
          <el-input v-model="form.cuCollectionAc" placeholder="请输入本次收款金额" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listSales, getSales, delSales, addSales, updateSales } from "@/api/crm/report/sales";

export default {
  name: "Sales",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 销售报表表格数据
      salesList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        salesman: null,
        collectionNo: null,
        collectionDate: null,
        customerName: null,
        source: null,
        sourceAc: null,
        sourceAcy: null,
        expenseTotal: null,
        salesPerform: null,
        productCost: null,
        commissionCost: null,
        targetAc: null,
        targetRate: null,
        percentRate: null,
        performAc: null,
        cuCollectionAc: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询销售报表列表 */
    getList() {
      this.loading = true;
      listSales(this.queryParams).then(response => {
        this.salesList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        salesman: null,
        collectionNo: null,
        collectionDate: null,
        customerName: null,
        source: null,
        sourceAc: null,
        sourceAcy: null,
        expenseTotal: null,
        salesPerform: null,
        productCost: null,
        commissionCost: null,
        targetAc: null,
        targetRate: null,
        percentRate: null,
        performAc: null,
        cuCollectionAc: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加销售报表";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getSales(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改销售报表";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateSales(this.form).then(response => {
              this.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addSales(this.form).then(response => {
              this.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$confirm('是否确认删除销售报表编号为"' + ids + '"的数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return delSales(ids);
        }).then(() => {
          this.getList();
          this.msgSuccess("删除成功");
        }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('crm/sales/export', {
        ...this.queryParams
      }, `crm_sales.xlsx`)
    }
  }
};
</script>
